package com.ruoyi.lipin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lipin.domain.bo.LpTagBo;
import com.ruoyi.lipin.domain.vo.LpTagVo;
import com.ruoyi.lipin.service.ILpTagService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 标签
 *
 * @author ruoyi
 * @date 2025-03-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lipin/tag")
public class LpTagController extends BaseController {

    private final ILpTagService iLpTagService;

    /**
     * 查询标签列表
     */
    @SaCheckPermission("lipin:tag:list")
    @GetMapping("/list")
    public TableDataInfo<LpTagVo> list(LpTagBo bo, PageQuery pageQuery) {
        return iLpTagService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出标签列表
     */
    @SaCheckPermission("lipin:tag:export")
    @Log(title = "标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LpTagBo bo, HttpServletResponse response) {
        List<LpTagVo> list = iLpTagService.queryList(bo);
        ExcelUtil.exportExcel(list, "标签", LpTagVo.class, response);
    }

    /**
     * 获取标签详细信息
     *
     * @param tagId 主键
     */
    @SaCheckPermission("lipin:tag:query")
    @GetMapping("/{tagId}")
    public R<LpTagVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long tagId) {
        return R.ok(iLpTagService.queryById(tagId));
    }

    /**
     * 新增标签
     */
    @SaCheckPermission("lipin:tag:add")
    @Log(title = "标签", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LpTagBo bo) {
        return toAjax(iLpTagService.insertByBo(bo));
    }

    /**
     * 修改标签
     */
    @SaCheckPermission("lipin:tag:edit")
    @Log(title = "标签", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LpTagBo bo) {
        return toAjax(iLpTagService.updateByBo(bo));
    }

    /**
     * 删除标签
     *
     * @param tagIds 主键串
     */
    @SaCheckPermission("lipin:tag:remove")
    @Log(title = "标签", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tagIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] tagIds) {
        return toAjax(iLpTagService.deleteWithValidByIds(Arrays.asList(tagIds), true));
    }
}
